package com.hrt.manage.product.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.ProductDetails;
import com.hrt.manage.product.dao.ProductDetailsDao;

@Repository
public class ProductDetailDaoImpl extends BaseDaoImpl<ProductDetails> implements
		ProductDetailsDao {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<ProductDetails> queryPageByCondition(List conditions,
			int pageIndex, int pageSize) {
		String hql = "select detail from ProductDetails detail";
		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", new Integer(0));
		conditions.add(delcon);
		for (int i = 0; i < conditions.size(); i++) {
			Map con = (Map) conditions.get(i);
			String field = con.get("field").toString();
			String val = con.get("value").toString();
			if (i == 0) {
				hql = hql + " where detail." + field + "=" + val;
			} else {
				hql = hql + " and detail." + field + "=" + val;
			}
		}
		List<ProductDetails> results = queryByPage(hql, pageIndex, pageSize);
		return results;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<ProductDetails> queryByCondition(List conditions) {
		String hql = "select detail from ProductDetails detail";
		Map delcon = new HashMap();
		delcon.put("field", "isdelete");
		delcon.put("value", new Integer(0));
		conditions.add(delcon);
		for (int i = 0; i < conditions.size(); i++) {
			Map con = (Map) conditions.get(i);
			String field = con.get("field").toString();
			String val = con.get("value").toString();
			if (i == 0) {
				hql = hql + " where detail." + field + "=" + val;
			} else {
				hql = hql + " and detail." + field + "=" + val;
			}
		}
		List<ProductDetails> results = this.getHibernateTemplate().find(hql);

		return results;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProductDetails> queryProducts(String itemCondition) {
		String hql = "select detail from ProductDetails detail where detail.isdelete=0 and (detail.proCode like '%"
				+ itemCondition
				+ "%'"
				+ " or detail.proName like '%"
				+ itemCondition + "%'" + ")";

		List<ProductDetails> results = new ArrayList<ProductDetails>();

		results = getHibernateTemplate().find(hql);
		return results;
	}

	/** 按是否删除查询 */
	@SuppressWarnings("unchecked")
	@Override
	public List<ProductDetails> findDelAll() {
		return getHibernateTemplate().find("FROM ProductDetails WHERE isDelete=0");
	}

	@Override
	public ProductDetails getDetailByProIdAndRuleId(final Long proId, final Long ruleId) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().execute(new HibernateCallback<ProductDetails>(){

			@Override
			public ProductDetails doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select detail from ProductDetails detail where detail.id="+ruleId+" and detail.proId="+proId+" and detail.isdelete=0";
				Query query=session.createQuery(hql);
				List result=query.list();
				if(result!=null && !result.isEmpty()){
					return (ProductDetails) result.get(0);
				}
				return null;
			}});
		
	}
	
	/**通过产品id查找产品规格*/
	@SuppressWarnings("unchecked")
	@Override
	public List<ProductDetails> getProDetailByProId(Long proId) {
		String hql = "select detail from ProductDetails detail where detail.isdelete=0 and detail.proId='"+proId+"'";
		return getHibernateTemplate().find(hql);
	}

	
	/**
	 * 获取产品规格库存
	 */
	@Override
	public Long getProInforDetailTotals(Long ruleId) {
		// TODO Auto-generated method stub
		final String hql="select detail.total from ProductDetails detail where detail.id="+ruleId+" and detail.isdelete=0";
		
		return getHibernateTemplate().execute(new HibernateCallback<Long>(){

			@Override
			public Long doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				
				Query query=session.createQuery(hql);
				return (Long) query.uniqueResult();
				
			}});
		
	
	}

	@Override
	public Long totalCount(Long proId) {
		String hql = "select count(detail) from ProductDetails detail where detail.isdelete=0 and detail.proId='"+proId+"'";
		return (Long) getHibernateTemplate().find(hql).get(0);
	}
	
}
